package com.jinzhi.jzweb.controller;


import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzapi.domain.UserDO;
import com.jinzhi.jzapi.service.UserService;
import com.jinzhi.jzapi.utils.JWTUtil;
import com.jinzhi.jzweb.domain.JazSysLabelDO;
import com.jinzhi.jzweb.domain.JazSysProjectDO;
import com.jinzhi.jzweb.domain.JazUserAuntDetailDO;
import com.jinzhi.jzweb.domain.vo.JazUserAuntVO;
import com.jinzhi.jzweb.service.*;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.JazUserAuntDO;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 月嫂管理
 * </pre>
 * <small> 2023-04-18 09:19:00 | Long</small>
 */
@Controller
@RequestMapping("/jzweb/jazUserAunt")
public class JazUserAuntController extends BaseController {
	@Autowired
	private JazUserAuntDetailService jazUserAuntDetailService;
	@Autowired
	private JazUserAuntService jazUserAuntService;
	@Autowired
	private JazSysLabelService jazSysLabelService;
	@Autowired
	private JazUserAuntPageService jazUserAuntPageService;
	@Autowired
	private UserService userService;
	@Autowired
	private JazSysProjectService jazSysProjectService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:jazUserAunt:jazUserAunt")
	String JazUserAunt(){
	    return "jzweb/jazUserAunt/jazUserAunt";
	}

	@GetMapping("/sleLabel")
	private Result<List<JazSysLabelDO>> sleLabel(){
		EntityWrapper<JazSysLabelDO> wrapper = new EntityWrapper<JazSysLabelDO>();
		wrapper.eq("status",0);//查询未禁用的标签
		List<JazSysLabelDO> jazSysLabelDOS = jazSysLabelService.selectList(wrapper);
		return Result.ok(jazSysLabelDOS);
	}


	@GetMapping("/sleProject")
	private Result<List<JazSysProjectDO>> sleProject(){
		EntityWrapper<JazSysProjectDO> wrapper = new EntityWrapper<JazSysProjectDO>();
		wrapper.eq("status",0);//查询未禁用的项目
		List<JazSysProjectDO> jazSysProjectDOS = jazSysProjectService.selectList(wrapper);
		return Result.ok(jazSysProjectDOS);
	}


	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:jazUserAunt:jazUserAunt")
	public Result<?> list(JazUserAuntVO jazUserAuntVO,String current,String size){
		Page<JazUserAuntVO> jazUserAuntVOPage = jazUserAuntPageService.selectUserAunt(jazUserAuntVO, Integer.valueOf(current), Integer.valueOf(size));
		return Result.ok(jazUserAuntVOPage);
	}


	@GetMapping("/add")
	@RequiresPermissions("jzweb:jazUserAunt:add")
	String add(){
	    return "jzweb/jazUserAunt/add";
	}

	@Log(value = "添加月嫂",sysModule = 2,operationType = 1)
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:jazUserAunt:add")
	public Result<String> save( JazUserAuntDetailDO jazUserAuntDetailDO) {
		//用户表插入数据
		UserDO userDO = new UserDO();
		userDO.setGmtCreate(new DateTime());
		try {
			BeanUtils.copyProperties(userDO,jazUserAuntDetailDO);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		//正常状态
		userDO.setStatus(1);
		// 设置为月嫂
		userDO.setType(1);
		//审核状态：未完善
		userDO.setState(3);
		userService.insert(userDO);

		//月嫂表插入数据
		JazUserAuntDO jazUserAuntDO = new JazUserAuntDO();
		jazUserAuntDO.setCreateTime(new DateTime());
		try {
			BeanUtils.copyProperties(jazUserAuntDO,jazUserAuntDetailDO);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		//与用户id绑定
		jazUserAuntDO.setSysUserid(userDO.getId());
		//默认展示
		jazUserAuntDO.setShowStatus(0);
		//默认待签约状态
		jazUserAuntDO.setSigningStatus(0);
		//默认待审核状态
		jazUserAuntDO.setAuditStatus(0);
		jazUserAuntService.insert(jazUserAuntDO);

		return Result.ok();
	}


	@GetMapping("/info/{id}")
	@RequiresPermissions("jzweb:jazUserAunt:jazUserAunt")
	String info(@PathVariable("id") Long id,Model model){
		//查询月嫂详情
		JazUserAuntDetailDO jazUserAuntDetailDO = jazUserAuntDetailService.selectAuntDetail(id);
		//查询月嫂类型
		String projectId = "";
		projectId = jazUserAuntService.selectById(id).getProjectId();
		List<String> projectIdList = Arrays.asList(projectId.split(","));
		List typeList = jazUserAuntDetailService.selectAuntType(projectIdList);
		String types = typeList.stream().map(String::valueOf).collect(Collectors.joining(",")).toString();
		jazUserAuntDetailDO.setType(types);
		//查询对应标签
		String labelId = "";
		labelId = jazUserAuntService.selectById(id).getLabelId();
		List<String> labelIdList = Arrays.asList(labelId.split(","));
		List describeList = jazUserAuntDetailService.selectAuntLable(labelIdList);
		String describes = describeList.stream().map(String::valueOf).collect(Collectors.joining(",")).toString();
		jazUserAuntDetailDO.setType(describes);

		model.addAttribute("jazUserAuntDetailDO", jazUserAuntDetailDO);
		return "jzweb/jazUserAunt/info";
	}


	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:jazUserAunt:edit")
	String edit(@PathVariable("id") Long id,Model model){
		//查询月嫂详情
		JazUserAuntDetailDO jazUserAuntDetailDO = jazUserAuntDetailService.selectAuntDetail(id);
		//查询月嫂类型
		String projectId = "";
		projectId = jazUserAuntService.selectById(id).getProjectId();
		List<String> projectIdList = Arrays.asList(projectId.split(","));
		List typeList = jazUserAuntDetailService.selectAuntType(projectIdList);
		String types = typeList.stream().map(String::valueOf).collect(Collectors.joining(",")).toString();
		jazUserAuntDetailDO.setType(types);
		//查询对应标签
		String labelId = "";
		labelId = jazUserAuntService.selectById(id).getLabelId();
		List<String> labelIdList = Arrays.asList(labelId.split(","));
		List describeList = jazUserAuntDetailService.selectAuntLable(labelIdList);
		String describes = describeList.stream().map(String::valueOf).collect(Collectors.joining(",")).toString();
		jazUserAuntDetailDO.setType(describes);

		model.addAttribute("jazUserAuntDetailDO", jazUserAuntDetailDO);
		model.addAttribute("id",id);
	    return "jzweb/jazUserAunt/edit";
	}
	
	@Log(value = "编辑并更新月嫂数据",sysModule = 2,operationType = 3)
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:jazUserAunt:edit")
	public Result<String>  update( JazUserAuntDetailDO jazUserAuntDetailDO,Long id){
		//更新月嫂表
		JazUserAuntDO jazUserAuntDO = new JazUserAuntDO();
		try {
			BeanUtils.copyProperties(jazUserAuntDO,jazUserAuntDetailDO);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		jazUserAuntDO.setId(id);
		jazUserAuntDO.setUpdateTime(new DateTime());
		boolean update = jazUserAuntService.updateById(jazUserAuntDO);

		//更新用户表
		UserDO userDO = new UserDO();
		try {
			BeanUtils.copyProperties(userDO,jazUserAuntDetailDO);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		userDO.setGmtModified(new DateTime());
		userDO.setId(jazUserAuntDO.getSysUserid());
		userService.updateById(userDO);

		return update ? Result.ok() : Result.fail();
	}
	
	@Log(value = "删除月嫂",sysModule = 2,operationType = 2)
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:jazUserAunt:remove")
	public Result<String>  remove( Long id){
		//删除月嫂数据
		jazUserAuntService.deleteById(id);
        return Result.ok();
	}


//	/**
//	 * 查看当前角色权限   0.普通用户  1.月嫂 (普通用户不现实简历刷新功能)
//	 * @return
//	 */
//	@GetMapping("/getUserRole")
//	@ResponseBody
//	public Result<?> getUserRole(){
//		UserDO getUser = JWTUtil.getUser();
//		UserDO userDO = userService.selectById(getUser.getId());
//		EntityWrapper<JazUserAuntDO> wrapper = new EntityWrapper<JazUserAuntDO>();
//		wrapper.eq("sys_userid",userDO.getId());
//		wrapper.eq("audit_result","0");
//		JazUserAuntDO jazUserAuntDO = jazUserAuntService.selectOne(wrapper);
//		if (jazUserAuntDO == null){    //如果 == null表示没有认证月嫂
//			return Result.ok(0);
//		}else {
//			return Result.ok(1);
//		}
//	}


	
}
